package solutions.leetcode.normal

/**
 * @author lizhidong
 * <a href="https://leetcode.cn/problems/minimum-number-of-operations-to-reinitialize-a-permutation/">1806. 还原排列的最少操作步数</a>
 */
class Solution1806 {
    fun reinitializePermutation(n: Int): Int {
        val perm = IntArray(n) { i -> i }
        println(perm.toList())
        val arr = perm.copyOf()
        var count = 0
        do {
            val arrCopy = arr.copyOf()
            for (i in 0 until n) {
                arr[i] = if (i % 2 == 0) {
                    arrCopy[i / 2]
                } else {
                    arrCopy[n / 2 + (i - 1) / 2]
                }
            }
            println(arr.toList())
            count++
        } while (!perm.contentEquals(arr))
        return count
    }
}

fun main() {
    for (i in 2 .. 20 step 2) {
        println("$i ---->")
        println(Solution1806().reinitializePermutation(i))
        println()
    }
}